/*
 * IMgui.java
 *
 * Created on March 26, 2008, 8:43 PM
 */

package imessenger;

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
 *
 * @author  Shelley Jane C. Co
 */
public class IMcontactsGui extends JFrame
{
    
    private IMClient imClient;
    private Vector<String> contacts = new Vector();
    private Hashtable onContact = new Hashtable();
    
    /** Creates new form IMgui */
    public IMcontactsGui(IMClient client)
    {
        this.imClient = client;
        this.setContacts(client.getAllContacts());
        initComponents();
        
        MouseListener mouseListener = new MouseAdapter() 
        {
            public void mouseClicked(MouseEvent e) 
            {
                if (e.getClickCount() == 2) 
                {
                    int in = contactList.locationToIndex(e.getPoint());
                    System.out.println("Double clicked on Item " + in);
                    String friend = contacts.elementAt(in);
                    if (friend.startsWith("[") && friend.endsWith("]"))
                    {
                        System.out.println("Group Name: "+friend);
                    }
                    else
                    {
                        if (onContact.containsKey(friend))
                        {
                            //System.out.println(onContact.get(friend).toString());
                            openOldChatWindow(onContact.get(friend));
                        }
                        else
                        {   openNewChatWindow(friend);  }
                    }
                }
            }
        };
        contactList.addMouseListener(mouseListener);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        contactList = new javax.swing.JList();
        signOut = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        contactList.setModel(new javax.swing.AbstractListModel() {

            int i = 0;
            String[] strings;
            //String temp = "", tab = "=>";
            public void putValue()
            {
                Iterator it = contacts.iterator();
                while (it.hasNext())
                {
                    //temp =
                    //if (!temp.startsWith("[") && !temp.endsWith("]"))
                    //{   strings[i] = tab.concat(temp); }
                    //else
                    //{
                        strings[i] = (String)it.next(); //temp; }

                    i++;
                }
            }
            public int getSize() { return contacts.size(); }
            public Object getElementAt(int i) { return contacts.elementAt(i); }

            Vector vec = contacts;
        });
        jScrollPane1.setViewportView(contactList);

        signOut.setText("Sign Off");
        signOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signOutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(signOut))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(signOut)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void signOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signOutActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        if (this.imClient.isLoggedIn)
            this.imClient.logout();
        
        //IMloginGui loginGui= new IMloginGui();
        Main.loginGui.setVisible(true);
}//GEN-LAST:event_signOutActionPerformed
    
    public void openNewChatWindow(String friend)
    {
        IMchatGui chatWindow = new IMchatGui(this.imClient);
        chatWindow.setVisible(true);
        chatWindow.setTitle(this.imClient.getSessionHost()+" - "+friend);
        chatWindow.setReceiver(friend);
        chatWindow.getSendTxtArea().setCaretPosition(0);
        this.imClient.getImListener().setChatGui(chatWindow);
        //IMListener imListener = new IMListener(this.imclient, chatWindow);
        //this.imclient.getYmSession().addSessionListener(imListener);
        
    }
    
    public boolean contactExists(String contact)
    {
        return this.onContact.containsKey(contact);
    }
    
    public void openOldChatWindow(Object value)
    {
        IMchatGui chatWindow = (IMchatGui)value;
        chatWindow.setVisible(true);        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList contactList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton signOut;
    // End of variables declaration//GEN-END:variables

    public Vector<String> getContacts() {
        return contacts;
    }

    public void setContacts(Vector<String> contacts) {
        this.contacts = contacts;
    }

    public Hashtable getOnContact() {
        return onContact;
    }
    
}
